﻿Option Explicit On
Imports AutoCAD
Imports System.Diagnostics.Process
<Serializable()> Public Class CAD_Class
    Public Section_Pts As List(Of TunnelSectionPoints)
    Dim Acad As AutoCAD.AcadApplication 'acad的主程序
    Dim AcadDoc As AcadDocument  'CAD的文档
    Dim AcadMo As AcadModelSpace  'CAD的空间,画图就在这里操作
    Dim cad_process As Process    '改CAD的进程,用完要结束掉
    Dim angels(5) As Double
    Dim arc_lists As List(Of AcadCircle)
    Public center As TSPCoor

    Public Sub New()

        ' Acad = CreateObject("AutoCAD.Application")
        Acad = New AutoCAD.AcadApplication()
        Acad.WindowState = AcWindowState.acMin
        Acad.Visible = False
        Dim now As Date = Date.Now
        Dim killprocess() As Process = GetProcessesByName("acad")

        For index As Integer = 0 To killprocess.Length - 1
            Dim span As TimeSpan = killprocess(index).StartTime - now
            If Math.Abs(span.Seconds) < 10 Then
                cad_process = killprocess(index)
            End If
        Next

        AcadDoc = Acad.ActiveDocument
        AcadMo = AcadDoc.ModelSpace
        'AcadDoc.WindowState = AcWindowState.acMin

    End Sub
    Public Sub config(ByVal section As tunnelSection)
        If Section_Pts.Count > 0 Then
            arc_lists = New List(Of AcadCircle)
            For i As Integer = 0 To Section_Pts.Count - 1
                Dim ct(2) As Double
                ct(0) = Section_Pts(i).center.Coor_H
                ct(1) = Section_Pts(i).center.Coor_V
                ct(2) = 0
                Dim arc As AcadCircle = AcadMo.AddCircle(ct, Section_Pts(i).Radius)
                arc_lists.Add(arc)
                If section.SectPnts.F_Note = False Then
                    If (i > 0) Then
                        angels(i) = (Section_Pts(i - 1).end_hv + Section_Pts(i).start_hv) / 2
                    End If
                Else

                    If (i > 0) Then
                        angels(i) = section.F_Section(i).AzimFrom(section.Four_circle_center)
                    End If
                End If
            Next
            angels(0) = 42.5 / 180 * Math.PI
            angels(5) = 317.5 / 180 * Math.PI
        End If
    End Sub

    Public Function OutPut_Result() As TunnelSectionPoints
        Dim pts As TunnelSectionPoints = New TunnelSectionPoints()
        F_Points = New TunnelSectionPoints()
        pts.center = center
        Dim jd As Double = angels(0)
        Dim sp(2) As Double
        sp(0) = center.Coor_H
        sp(1) = center.Coor_V
        sp(2) = 0
        Dim count As Integer = 1
        For i As Integer = 0 To 4
            Do While jd <= angels(i + 1)
                Dim ep(2) As Double

                ep(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : ep(1) = sp(1) + 100 * Math.Sin(jd - Math.PI / 2) : ep(2) = 0
                Dim pl As AcadLine = AcadMo.AddLine(sp, ep)
                Dim intersect(2) As Double
                intersect = arc_lists(i).IntersectWith(pl, AcExtendOption.acExtendThisEntity)

                Dim pt As New SectionPoint(count.ToString, jd, "1")
                If intersect.Length = 0 Then
                    pt.Coor = New TSPCoor(0, 0)
                Else
                    pt.Coor = New TSPCoor(intersect(1), intersect(0))
                End If

                pts.Points.Add(pt)
                count += 1
                jd += 5 / 180 * Math.PI
            Loop
            If i < 4 Then
                jd = angels(i + 1)
                Dim e(2) As Double
                e(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : e(1) = sp(1) + 100 * Math.Sin(jd - Math.PI / 2) : e(2) = 0
                Dim F_line As AcadLine = AcadMo.AddLine(sp, e)
                Dim inser(2) As Double
                inser = arc_lists(i).IntersectWith(F_line, AcExtendOption.acExtendThisEntity)
                Dim pppp As New SectionPoint(i.ToString(), 0, 0)
                If inser.Length = 0 Then
                    pppp.Coor = New TSPCoor(0, 0)
                Else
                    pppp.Coor = New TSPCoor(inser(1), inser(0))
                End If
                F_Points.Points.Add(pppp)
            End If
        Next
        pts.Call_Arc_Err()
        Return pts
    End Function
    Public F_Points As TunnelSectionPoints '用来储存接缝处的坐标,一共有4组

    Public Function Out_caculation(ByVal F_Section As List(Of SectionPoint)) As Double()
        Dim sp(2) As Double
        sp(0) = center.Coor_H
        sp(1) = center.Coor_V
        sp(2) = 0
        Dim re(3) As Double
        For i As Integer = 1 To F_Section.Count - 2
            Dim ep(2) As Double
            Dim jd As Double = F_Section(i).AzimFrom(center)
            ep(0) = sp(0) + 100 * Math.Cos(jd - Math.PI / 2) : ep(1) = sp(0) + 100 * Math.Sin(jd - Math.PI / 2) : ep(2) = 0
            Dim pl As AcadLine = AcadMo.AddLine(sp, ep)
            Dim intersect1(2) As Double
            Dim intersect2(2) As Double
            intersect1 = arc_lists(i - 1).IntersectWith(pl, AcExtendOption.acExtendThisEntity)
            intersect2 = arc_lists(i).IntersectWith(pl, AcExtendOption.acExtendThisEntity)
            Dim mDe As Double = intersect1(0) - intersect2(0)
            Dim mDn As Double = intersect1(1) - intersect2(1)
            re(i - 1) = Math.Sqrt(mDe * mDe + mDn * mDn) * 1000
        Next
        Return re

    End Function

    '把CAD的进程Kill掉,免得开一堆
    Public Sub Dispose()
        Try
            cad_process.Kill()
        Catch ex As Exception
            Return
        End Try

    End Sub

End Class
